package com.snowfield.lb47.lbconfig.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteListDetail;
import com.snowfield.lb47.lbconfig.service.IBlackwhiteListDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 黑白名单详情Controller
 *
 * @author snowfield
 * @date 2024-05-24
 */
@RestController
@RequestMapping("/lbconfig/blackwhitelistDetail")
public class BlackwhiteListDetailController extends BaseController {
    @Autowired
    private IBlackwhiteListDetailService blackwhiteListDetailService;

    /**
     * 查询黑白名单详情列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelistDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlackwhiteListDetail blackwhiteListDetail) {
        startPage();
        List<BlackwhiteListDetail> list = blackwhiteListDetailService.selectBlackwhiteListDetailList(blackwhiteListDetail);
        return getDataTable(list);
    }

    /**
     * 导出黑白名单详情列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelistDetail:export')")
    @Log(title = "黑白名单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlackwhiteListDetail blackwhiteListDetail) {
        List<BlackwhiteListDetail> list = blackwhiteListDetailService.selectBlackwhiteListDetailList(blackwhiteListDetail);
        ExcelUtil<BlackwhiteListDetail> util = new ExcelUtil<BlackwhiteListDetail>(BlackwhiteListDetail.class);
        util.exportExcel(response, list, "黑白名单详情数据");
    }

    /**
     * 获取黑白名单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelistDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(blackwhiteListDetailService.selectBlackwhiteListDetailById(id));
    }

    /**
     * 新增黑白名单详情
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelistDetail:add')")
    @Log(title = "黑白名单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlackwhiteListDetail blackwhiteListDetail) {
        if (!blackwhiteListDetailService.checkIpUnique(blackwhiteListDetail)) {
            return AjaxResult.error("新增IP地址 '" + blackwhiteListDetail.getIpaddr() + "'失败，IP地址已存在");
        }
        return toAjax(blackwhiteListDetailService.insertBlackwhiteListDetail(blackwhiteListDetail));
    }

    /**
     * 修改黑白名单详情
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelistDetail:edit')")
    @Log(title = "黑白名单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlackwhiteListDetail blackwhiteListDetail) {
        if (!blackwhiteListDetailService.checkIpUnique(blackwhiteListDetail)) {
            return AjaxResult.error("修改IP地址 '" + blackwhiteListDetail.getIpaddr() + "'失败，IP地址已存在");
        }
        return toAjax(blackwhiteListDetailService.updateBlackwhiteListDetail(blackwhiteListDetail));
    }

    /**
     * 删除黑白名单详情
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelistDetail:remove')")
    @Log(title = "黑白名单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{listId}/{ids}")
    public AjaxResult remove(@PathVariable("listId") Long listId, @PathVariable("ids") Long[] ids) {
        return toAjax(blackwhiteListDetailService.deleteBlackwhiteListDetailByIds(listId, ids));
    }
}
